package com.sren.initializrstart.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sren.initializrstart.service.WeatherDataService;
import com.sren.initializrstart.util.StringUtil;
import com.sren.initializrstart.vo.WeatherResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author sren
 * @create 2018-12-06 下午4:27
 **/

@Service
@Slf4j
public class WeatherDataServiceImpl implements WeatherDataService {

    private static final String BASE_URL = "http://wthrcdn.etouch.cn/weather_mini";

    private static final Long TIME_OUT = 1800L;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public WeatherResponse getDataByCityId(String cityId) {
        String uri = BASE_URL + "?citykey=" + cityId;
        return getWeatherResponse(uri);
    }

    @Override
    public WeatherResponse getDataByCityName(String cityName) {
        String uri = BASE_URL + "?city=" + cityName;
        return getWeatherResponse(uri);
    }

    @Override
    public void syncDataByCityId(String cityId) {
        String uri = BASE_URL + "?citykey=" + cityId;
        saveWeatherData(uri);
    }

    @Override
    public void syncDataByCityName(String cityName) {
        String uri = BASE_URL + "?city=" + cityName;
        saveWeatherData(uri);
    }

    /**
     * 缓存插入数据
     * @param uri
     */
    private void saveWeatherData(String uri) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ResponseEntity<String> entity = restTemplate.getForEntity(uri, String.class);
        if (entity.getStatusCodeValue() != 200) {
            return;
        }
        try{
            String result = StringUtil.conventFromGzip(entity.getBody());
            ops.set(uri, result, TIME_OUT, TimeUnit.SECONDS);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private WeatherResponse getWeatherResponse(String uri) {
        String key = uri;
        String result = null;
        WeatherResponse weatherResponse = null;
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();

        if (stringRedisTemplate.hasKey(key)) {
            String value = ops.get(key);
            try {
                weatherResponse = objectMapper.readValue(value, WeatherResponse.class);
                log.info("redis has data");
            } catch (IOException e) {
                e.printStackTrace();
            }

            return weatherResponse;
        }
        ResponseEntity<String> entity = restTemplate.getForEntity(uri, String.class);

        if (entity.getStatusCodeValue() != 200) {
            return null;
        }

        try{
            result = StringUtil.conventFromGzip(entity.getBody());
            weatherResponse = objectMapper.readValue(result, WeatherResponse.class);

            ops.set(key, result, TIME_OUT, TimeUnit.SECONDS);
            log.info("redis set data");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return weatherResponse;
    }
}
